package com.example.jsf.controller;

import com.example.jsf.Repository.GoodsRepository;
import com.example.jsf.Repository.IncomeRepository;
import com.example.jsf.entity.Goods;
import com.example.jsf.entity.Income;
import com.example.jsf.other.DataHelper;
import com.example.jsf.other.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("upTest")
    public Result upTest(int id, int price){
        int i = jdbcTemplate.update("UPDATE goods SET goods_price = ? WHERE id = ?", new Object[]{price, id});
        return Result.success(i);
    }

    @PostMapping("addGoods")
    public Result addGoods(@RequestBody Goods goods) {
        goods.setCreateTime(new Date());
        return  Result.success(goodsRepository.save(goods));
    }

    @GetMapping("getGoods")
    public Result getGoods(
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize",defaultValue = "2") int pageSize
    ) {
        String s = DataHelper.selectSQL(Goods.class);

//        s+=" order by create_time desc";

        System.out.println("s = " + s);
        return Result.success(DataHelper.pageQuery(jdbcTemplate,s,pageNo,pageSize,Goods.class));

    }
}
